package speed2010.reformulacao.query;

import java.io.IOException;
import java.util.HashMap;

import speed2010.reformulacao.useful.Standards;

public class Log {
	
	StringBuffer reformulationLog;
	StringBuffer answerLog;
	
	public Log() {
		reformulationLog = new StringBuffer();
		answerLog = new StringBuffer();
	}

	// Build the log format - original query
	public void loadOriginalQueryDL(String originalQuery, String mode,
			String reformulationVariables, boolean approximate,
			boolean generalize, boolean compose, boolean specialize) throws IOException {

		reformulationLog.append("\n");
		reformulationLog.append("\n");
		reformulationLog.append("----------------------------------------------------------------------------");
		reformulationLog.append("\n");
		reformulationLog.append("Query Reformulation Mode: " + mode);
		reformulationLog.append("\n");

		reformulationLog.append("Using Enriching Variables: "
				+ reformulationVariables);
		if (reformulationVariables.equals("Yes")) {
			reformulationLog.append("\n");
			reformulationLog.append("Selected Variables: ");
			if (approximate) {
				reformulationLog.append("\n");
				reformulationLog.append("- Approximate");
			}
			if (generalize) {
				reformulationLog.append("\n");
				reformulationLog.append("- Generalize");
			}
			if (compose) {
				reformulationLog.append("\n");
				reformulationLog.append("- Compose");
			}
			if (specialize) {
				reformulationLog.append("\n");
				reformulationLog.append("- Specialize");
			}

			reformulationLog.append("\n");
			reformulationLog.append("\nPriority of the Variables: ");
			String priorityTemp = "";
			if (Standards.variablesPriority.size() > 0) {
				for (int i = 0; i < Standards.variablesPriority.size(); i++) {
					priorityTemp = priorityTemp
							+ Standards.variablesPriority.get(i).toString()
							+ ", ";
				}
			}
			if (!priorityTemp.equals("")) {
				priorityTemp = priorityTemp.substring(0,
						priorityTemp.length() - 2);
			}
			reformulationLog.append(priorityTemp);
		}

		reformulationLog.append("\n");
		reformulationLog.append("\n");
		reformulationLog.append("Original Query (Source Peer): "
				+ originalQuery);
		reformulationLog.append("\n");

		answerLog.append("\n");
		answerLog.append("\n");
		answerLog.append("----------------------------------------------------------------------------\n");
		answerLog.append("Original Query (Source Peer): " + originalQuery);
		answerLog.append("\n");

	}

	// Build the log format - original query with properties
	public void loadOriginalQueryDLProperty(String originalQuery)
			throws IOException {

		reformulationLog.append("\n");
		reformulationLog.append("\n");
		reformulationLog.append("----------------------------------------------------------------------------");
		reformulationLog.append("\n");

		reformulationLog.append("Original Query (Source Peer): "
				+ originalQuery);
		reformulationLog.append("\n");

		answerLog.append("\n");
		answerLog.append("\n");
		answerLog.append("----------------------------------------------------------------------------\n");
		answerLog.append("Original Query (Source Peer): " + originalQuery);
		answerLog.append("\n");

	}

	// Build the log format - reformulated query with properties
	public void loadReformulatedQueriesProperties(String query) {
		reformulationLog.append("\n");
		reformulationLog.append("\n");

		reformulationLog.append("Reformulated Query (Target Peer): " + query);
		reformulationLog.append("\n");

		answerLog.append("\n");
		answerLog.append("\n");
		answerLog.append("Reformulated Query (Target Peer): " + query);
		answerLog.append("\n");
	}

	// Build the log format - reformulated query
	public void loadExactEnrichedQueriesDL(HashMap exactEnrichedQueries, String type) throws IOException {

		boolean enriched = false;

		reformulationLog.append("\nExact Query (Target Peer): "
				+ exactEnrichedQueries.get("EXACT_SOURCE").toString());

		if (Standards.complete == true
				&& (Standards.approximate == true
						|| Standards.generalize == true
						|| Standards.compose == true || Standards.specialize == true)) {
			enriched = true;
		} else if ((Standards.approximate == true
				|| Standards.generalize == true || Standards.compose == true || Standards.specialize == true)
				&& Standards.restricted == true) {
			if (exactEnrichedQueries.get("EXACT_SOURCE").equals("")) {
				enriched = true;
			}
		}

		reformulationLog.append("\n");
		reformulationLog.append("\nEnriched Query (Target Peer - Negation): "
				+ exactEnrichedQueries.get("negation").toString());

		if (enriched == true) {

			reformulationLog.append("\n");
			reformulationLog.append("\nEnriched Query (Target Peer - Approximate): "
							+ exactEnrichedQueries.get("approximate")
									.toString());

			reformulationLog.append("\n");
			reformulationLog.append("\nEnriched Query (Target Peer - Generalize): "
							+ exactEnrichedQueries.get("generalize").toString());

			reformulationLog.append("\n");
			reformulationLog.append("\nEnriched Query (Target Peer - Specialize): "
							+ exactEnrichedQueries.get("specialize").toString());

			reformulationLog.append("\n");
			reformulationLog.append("\nEnriched Query (Target Peer - Compose): "
							+ exactEnrichedQueries.get("compose").toString());

		}
		answerLog.append("\n");
		answerLog.append("Executed Queries:\n");
		if (!exactEnrichedQueries.get("EXACT_SOURCE").toString().equals("")) {
			answerLog.append("\nExact query: "
					+ exactEnrichedQueries.get("EXACT_SOURCE").toString());
			answerLog.append("\n");
		}
		if (!exactEnrichedQueries.get("negation").toString().equals("")) {
			answerLog.append("\nNegation query: "
					+ exactEnrichedQueries.get("negation").toString());
			answerLog.append("\n");
		}
		if (!exactEnrichedQueries.get("approximate").toString().equals("")) {
			answerLog.append("\nApproximation query: "
					+ exactEnrichedQueries.get("approximate").toString());
			answerLog.append("\n");
		}
		if (!exactEnrichedQueries.get("generalize").toString().equals("")) {
			answerLog.append("\nGeneralization query: "
					+ exactEnrichedQueries.get("generalize").toString());
			answerLog.append("\n");
		}
		if (!exactEnrichedQueries.get("specialize").toString().equals("")) {
			answerLog.append("\nSpecialization query: "
					+ exactEnrichedQueries.get("specialize").toString());
			answerLog.append("\n");
		}
		if (!exactEnrichedQueries.get("compose").toString().equals("")) {
			answerLog.append("\nComposition query: "
					+ exactEnrichedQueries.get("compose").toString());
			answerLog.append("\n");
		}

		answerLog.append("\n");
		answerLog.append("Priority of the Variables: ");
		String priorityTemp = "";
		if (Standards.variablesPriority.size() > 0) {
			for (int i = 0; i < Standards.variablesPriority.size(); i++) {
				priorityTemp = priorityTemp
						+ Standards.variablesPriority.get(i).toString() + ", ";
			}
		}
		if (!priorityTemp.equals("")) {
			priorityTemp = priorityTemp.substring(0, priorityTemp.length() - 2);
		}
		answerLog.append(priorityTemp);

	}

}
